package org.easyway.tiles.generic;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.MemoryImageSource;
import java.awt.image.PixelGrabber;
import java.io.FileInputStream;

import javax.swing.BorderFactory;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;

public class TextureButton extends JPanel implements ActionListener {
	private static final long serialVersionUID = 1201422103111406229L;

	transient Image img; // declare the image

	private String type = "$_TILE";

	public int[] img_pixels;

	public String filename;

	private transient JPopupMenu popup;

	public static transient GenericMapEditor gme;

	private int width, height;

	public CustomImage tile;

	public TextureButton( final CustomImage tile ) {
		filename = tile.filename;
		img = tile.img;
		type = tile.type;
		width = GraphicBoard.tileWidth;
		height = GraphicBoard.tileHeight;
		this.tile = tile;	
		this.setBounds(this.getX(), this.getY(), GraphicBoard.tileWidth,
				GraphicBoard.tileHeight);
		setPreferredSize(new Dimension(GraphicBoard.tileWidth,
				GraphicBoard.tileHeight));
		this.setVisible(true);
		this.setBorder(BorderFactory.createLineBorder(Color.black, 1));
		addMouseListener(new java.awt.event.MouseAdapter() {
			public void mousePressed(java.awt.event.MouseEvent e) {
				GenericMapEditor.isTool = false;
				// GenericMapEditor.currentJPanel.cimg.img = getInstance().img;
				// GenericMapEditor.currentJPanel.cimg.filename =
				// getInstance().filename;
				// GenericMapEditor.currentJPanel.cimg.type =
				// getInstance().type;
				GenericMapEditor.currentJPanel.cimg = tile;
				GenericMapEditor.currentJPanel.image = tile.img;
				GenericMapEditor.currentJPanel.repaint();
			}
		});

		// Create the popup menu.
		popup = new JPopupMenu();
		JMenuItem menuItem = new JMenuItem("Remove this texture");
		menuItem.addActionListener(this);
		popup.add(menuItem);

		menuItem = new JMenuItem("Set type of texture");
		menuItem.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {
				TypeSelector ts = new TypeSelector(gme, getInstance());
				ts.setVisible(true);
			}
		});
		popup.add(menuItem);

		// Add listener to components that can bring up popup menus.
		MouseListener popupListener = new PopupListener();
		this.addMouseListener(popupListener);
	}
	
	public TextureButton(String img_filename) {
		filename = img_filename;
		//this.gme = gme;
		img = Toolkit.getDefaultToolkit().getImage(img_filename);

		// Get info of the image
		ImageInfo ii = new ImageInfo();

		try {
			ii.setInput(new FileInputStream(img_filename));

			if (!ii.check())
				System.err.println("Not a supported image file format.");
			else {
				width = ii.getWidth();
				height = ii.getHeight();
			}
		} catch (Exception e) {
		}

		System.out.println(width + ":" + height);

		this.setBounds(this.getX(), this.getY(), GraphicBoard.tileWidth,
				GraphicBoard.tileHeight);
		setPreferredSize(new Dimension(GraphicBoard.tileWidth,
				GraphicBoard.tileHeight));
		this.setVisible(true);
		this.setBorder(BorderFactory.createLineBorder(Color.black, 1));
		addMouseListener(new java.awt.event.MouseAdapter() {
			public void mousePressed(java.awt.event.MouseEvent e) {
				GenericMapEditor.isTool = false;
				// GenericMapEditor.currentJPanel.cimg.img = getInstance().img;
				// GenericMapEditor.currentJPanel.cimg.filename =
				// getInstance().filename;
				// GenericMapEditor.currentJPanel.cimg.type =
				// getInstance().type;
				GenericMapEditor.currentJPanel.cimg = tile;
				GenericMapEditor.currentJPanel.image = tile.img;
				GenericMapEditor.currentJPanel.repaint();
			}
		});

		// Create the popup menu.
		popup = new JPopupMenu();
		JMenuItem menuItem = new JMenuItem("Remove this texture");
		menuItem.addActionListener(this);
		popup.add(menuItem);

		menuItem = new JMenuItem("Set type of texture");
		menuItem.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {
				TypeSelector ts = new TypeSelector(gme, getInstance());
				ts.setVisible(true);
			}
		});
		popup.add(menuItem);

		// Add listener to components that can bring up popup menus.
		MouseListener popupListener = new PopupListener();
		this.addMouseListener(popupListener);

		tile = new CustomImage();
		tile.filename = filename;
		tile.type = type;
		tile.img = img;
	}

	public void setUpFromSerialization() {
		img = getImageFromArray(img_pixels);
		tile.img = img;
		if (tile.type != type) {
			tile.type = this.type;
		}

		this.setBounds(this.getX(), this.getY(), GraphicBoard.tileWidth,
				GraphicBoard.tileHeight);
		setPreferredSize(new Dimension(GraphicBoard.tileWidth,
				GraphicBoard.tileHeight));
		this.setVisible(true);
		this.setBorder(BorderFactory.createLineBorder(Color.black, 1));
		addMouseListener(new java.awt.event.MouseAdapter() {
			public void mousePressed(java.awt.event.MouseEvent e) {
				GenericMapEditor.isTool = false;
				GenericMapEditor.currentJPanel.cimg = tile;
				GenericMapEditor.currentJPanel.image = tile.img;
				GenericMapEditor.currentJPanel.repaint();
			}
		});

		// Create the popup menu.
		popup = new JPopupMenu();
		JMenuItem menuItem = new JMenuItem("Remove this texture");
		menuItem.addActionListener(this);
		popup.add(menuItem);

		menuItem = new JMenuItem("Set type of texture");
		menuItem.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {
				TypeSelector ts = new TypeSelector(gme, getInstance());
				ts.setVisible(true);
			}
		});
		popup.add(menuItem);

		// Add listener to components that can bring up popup menus.
		MouseListener popupListener = new PopupListener();
		this.addMouseListener(popupListener);
	}

	public void actionPerformed(ActionEvent e) {
		gme.textureArea.remove(this);
		gme.textureArea.validate();
		gme.repaint();
	}

	public TextureButton getInstance() {
		return this;
	}

	public void paintComponent(Graphics g) {
		g.drawImage(img, 0, 0, GraphicBoard.tileWidth, GraphicBoard.tileHeight,
				this);
	}

	class PopupListener extends MouseAdapter {
		public void mousePressed(MouseEvent e) {
			maybeShowPopup(e);
		}

		public void mouseReleased(MouseEvent e) {
			maybeShowPopup(e);
		}

		private void maybeShowPopup(MouseEvent e) {
			if (e.isPopupTrigger()) {
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		}
	}

	public int[] getArrayFromImage(Image img) {
		int[] pixels = new int[width * height];
		try {

			PixelGrabber pg = new PixelGrabber(img, 0, 0, width, height,
					pixels, 0, width);
			pg.grabPixels();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return pixels;
	}

	public Image getImageFromArray(int[] pixels) {
		MemoryImageSource mis = new MemoryImageSource(width, height, pixels, 0,
				width);
		Toolkit tk = Toolkit.getDefaultToolkit();
		return tk.createImage(mis);
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.tile.type = this.type = type;
	}
	
	public boolean equals( Object obj ) {
		if (obj instanceof TextureButton) {
			return ((TextureButton)obj).tile.equals(tile);
		}
		if  ( obj instanceof CustomImage ) {
			return ((CustomImage)obj).equals(tile);
		}
		return obj.equals(this);
	}

}
